﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LifeTree.DomainModel.Repositories;
using System.Data.Entity;
using LifeTree.DomainModel.Entities;

namespace LifeTree.EntityFrameworkProvider.Repositories
{
    public class GenericRepository<T> : IRepository<T> where T : Entity
    {
        private readonly ILifeTreeContext dataContext;
        private readonly IDbSet<T> dbSet;

        protected IDbSet<T> DbSet
        {
            get { return this.dbSet; }
        }

        public GenericRepository(ILifeTreeContext dataContext)
        {
            this.dataContext = dataContext;
            dbSet = dataContext.GetSetByType<T>();
        }

        public void Add(T entity)
        {
            dbSet.Add(entity);
        }

        public void Remove(T entity)
        {
            dbSet.Remove(entity);
        }

        public T GetById(string id)
        {
            return dbSet.FirstOrDefault(e => e.Id == id);
        }

        public IEnumerable<T> GetAll()
        {
            return dbSet;
        }
    }
}
